﻿using Newtonsoft.Json;
using StackExchange.Redis;

namespace Manager.Database
{
    public class RedisServer
    {
        int dbIndex = 0;
        ConnectionMultiplexer redis;
        public readonly IDatabase DefaultDb;


        public RedisServer(IRedisSettings settings)
        {
            redis = ConnectionMultiplexer.Connect(settings.ConnectionString);
            DefaultDb = redis.GetDatabase(settings.DatabaseIndex);
            dbIndex = settings.DatabaseIndex;
        }

        /// <summary>
        /// 根据index获取redis数据库 
        /// </summary>
        /// <param name="dbIndex"></param>
        /// <returns></returns>
        public IDatabase GetDatabase(int dbIndex)
        {
            return redis.GetDatabase(dbIndex);
        }


        /// <summary>
        /// 获取key里面的value值，转为double值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public double GetValue(string key)
        {
            if (DefaultDb.KeyExists(key))
            {
                var resultStr = DefaultDb.StringGet(key);
                if (resultStr.HasValue)
                {
                    var result = JsonConvert.DeserializeObject<DevDataItem>(resultStr);
                    double value = Convert.ToDouble(result.Value);
                    return value;
                }
                else
                {
                    throw new Exception("未连接电子秤！" + key);
                }
            }
            else
            {
                throw new Exception("未连接电子秤！" + key);
            }
        }


        /// <summary>
        /// redis 存储的实时数据结构
        /// </summary>
        public class DevDataItem
        {
            /// <summary>
            /// 数据名称
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// 设备Id
            /// </summary>
            public string DevId { get; set; }

            /// <summary>
            /// 数据唯一标识
            /// </summary>
            public string DevDataId { get; set; }
            /// <summary>
            /// 数据
            /// </summary>
            public object Value { get; set; }

            /// <summary>
            /// 原始时间戳
            /// </summary>
            public long TimeStamp { get; set; }

            /// <summary>
            /// 采集时间
            /// </summary>
            public DateTime CollectTime { get; set; }

            /// <summary>
            /// 处理时间
            /// </summary>
            public DateTime ProcessTime { get; set; }

            /// <summary>
            /// 错误标志
            /// </summary>
            public ErrorState ErrorState { get; set; }
        }


        /// <summary>
        /// 采集端上传的错误标志信息
        /// </summary>
        public enum ErrorState
        {
            /// <summary>
            /// 无错误
            /// </summary>
            NoErr,
            /// <summary>
            /// 读取错误
            /// </summary>
            ReadErr,
            /// <summary>
            /// 处理错误
            /// </summary>
            ProcessErr
        }

    }
}
